package io.zhifei.modules.sys.pr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.*;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.common.utils.Query;
import io.zhifei.common.utils.R;
import io.zhifei.modules.sys.pr.entity.XxlJobInfo;
import io.zhifei.modules.sys.pr.model.params.XxlJobInfoParam;
import io.zhifei.modules.sys.pr.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 任务控制器
 *
 * @author qdp
 * @Date 2021-08-09 14:40:34
 */
@Controller
@RequestMapping("/job")
@Api(value = "任务控制器",description = ("任务控制器"))
public class JobController {

    @Autowired
    private JobService jobService;

    /**
     * 新增接口
     *
     * @author duke
     * @date 2021-08-09
     */
    @PostMapping("/addItem")
    @ApiOperation(value = "新增任务接口", notes = "新增接口")
    @ResponseBody
    public R addItem(@RequestBody XxlJobInfo xxlJobInfo) {
        this.jobService.add(xxlJobInfo);
        return R.ok();
    }

    /**
     * 编辑接口
     *
     * @author qdp
     * @date 2021-08-09
     */
    @PostMapping("/editItem")
    @ApiOperation(value = "编辑任务接口", notes = "编辑接口")
    @ResponseBody
    public R editItem(@RequestBody XxlJobInfo xxlJobInfo) {
        this.jobService.updateById(xxlJobInfo);
        return R.ok();
    }

    /**
     * 查看任务列表
     *
     * @author qdp
     * @Date 2021-08-09
     */
    @ResponseBody
    @GetMapping("/taskList")
    @ApiOperation("查看任务列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", paramType = "query", required = true),
            @ApiImplicitParam(name = "limit", value = "当前页大小", paramType = "query", required = true),
    })
    @ApiResponses({
            @io.swagger.annotations.ApiResponse(code = 200, message = "请求成功", response = XxlJobInfo.class)
    })
    public R list(@RequestParam Map<String, Object> pageParam) {
        return R.ok().put("page", new PageUtils(this.jobService.page(new Query<XxlJobInfo>().getPage(pageParam), Wrappers.<XxlJobInfo>query().lambda().isNull(XxlJobInfo::getChildJobId))));
    }


    /**
     * 新增或者更新调度任务接口
     *
     * @author qdp
     * @date 2021-08-09
     */
    @PostMapping("/submitSchedu")
    @ApiOperation(value = "新增或者更新调度任务接口", notes = "新增接口")
    @ResponseBody
    public R addSchedu(@RequestBody XxlJobInfoParam xxlJobInfoParam) {
        if(this.jobService.addSchedu(xxlJobInfoParam))return R.ok();
        else return R.error();
    }

    /**
     * 查看调度列表
     *
     * @author qdp
     * @Date 2021-08-09
     */
    @GetMapping("/getScheduPageList")
    @ApiOperation(value = "查询分页列表", notes = "查询分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", paramType = "query", required = true),
            @ApiImplicitParam(name = "limit", value = "当前页大小", paramType = "query", required = true),
    })
    @ApiResponses({
            @io.swagger.annotations.ApiResponse(code = 200, message = "请求成功", response = XxlJobInfo.class)
    })
    @ResponseBody
    public R getScheduPageList(@RequestParam Map<String, Object> pageParam) {
        QueryWrapper<XxlJobInfo> qw = new QueryWrapper<>();
        IPage<XxlJobInfo> page = new Query<XxlJobInfo>().getPage(pageParam);
        pageParam.remove("page");
        pageParam.remove("limit");
        qw.allEq(pageParam);
        IPage<XxlJobInfoParam> p = this.jobService.scheduPage(page, qw);
        return R.ok().put("page", new PageUtils(p));
    }

}
